package com.example.androidsx;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class ProductManagementActivity extends AppCompatActivity {
    private SQLcj dbHelpr;
    private ListView pt_list;
    private SimpleCursorAdapter pt_adapter;
    private Button addprodcut;
     private EditText etmoney,etnumber,ettype;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.productmanagement);


        dbHelpr=new SQLcj(this);
        pt_list=findViewById(R.id.product_list);

        loaUserdata();

        Button btnBack = findViewById(R.id.btn_back);
        addprodcut=findViewById(R.id.btn_add_product);
        addprodcut.setOnClickListener(v->{
            ShowInputDialog();
        });


        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(ProductManagementActivity.this, gly.class);


                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);


                startActivity(intent);


                finish();
            }
        });
    }
    private void loaUserdata(){
        try {
            Cursor cursor= dbHelpr.getAllProducts();
            String[] fromColumns={
                    SQLcj.COLUMN_MONEY,
                    SQLcj.COLUMN_NUMBER,
                    SQLcj.COLUMN_TYPE
            };
            int[] toViews={
                    R.id.pt_money,
                    R.id.pt_number,
                    R.id.pt_type
            };


            pt_adapter=new SimpleCursorAdapter(
                    this,
                    R.layout.listview_product,
                    cursor,
                    fromColumns,
                    toViews,
                    0
            );

            pt_list.setAdapter(pt_adapter);
            pt_list.setOnItemClickListener((parent, view, position, id) -> {
                Cursor item=(Cursor) pt_adapter.getItem(position);
                if (item.moveToPosition(position)){
                    String type=item.getString(item.getColumnIndexOrThrow(SQLcj.COLUMN_TYPE));
                    Toast.makeText(this,"选中商品："+type,Toast.LENGTH_SHORT).show();
                }
            });
        } catch (Exception e) {
            Toast.makeText(this,"商品列表加载失败",Toast.LENGTH_SHORT).show();
            Log.e("Suppliermanagement","数据库错误",e);
        }
    }
    private void ShowInputDialog() {

        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_input, null);
         etmoney   = dialogView.findViewById(R.id.et_money);
        etnumber = dialogView.findViewById(R.id.et_number);
        ettype   = dialogView.findViewById(R.id.et_type);



        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("输入商品信息")
                .setView(dialogView)
                .setPositiveButton("保存", (dialogInterface, which) -> {
                    // 保存逻辑
                    String money = etmoney.getText().toString();
                    String number = etnumber.getText().toString();
                    String type = ettype.getText().toString();

                    try {
                        double money1 = Double.parseDouble(money);
                        int number1 = Integer.parseInt(number);
                        long id = dbHelpr.addPradut(money1, number1, type);
                        if (id != -1) {
                            Toast.makeText(this, "商品添加成功。", Toast.LENGTH_SHORT).show();
                            loaUserdata();
                        }
                    } catch (NumberFormatException e) {
                        Toast.makeText(this, "请输入有效的数字", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", null)
                .create();


        Window window = dialog.getWindow();
        if (window != null) {
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }

        dialog.show();


        }

private  boolean isKeyboardActive(){
        InputMethodManager imm=(InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        return imm.isActive(ettype);
}
    private void showKeyboardForced(final EditText editText) {
        editText.setInputType(InputType.TYPE_CLASS_TEXT); // 强制使用普通文本输入
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

    }

    @Override
    protected void onDestroy() {
        if(pt_adapter!=null);{
            Cursor cursor = pt_adapter.getCursor();
            if(cursor != null) cursor.close();
        }
        if (dbHelpr!=null) dbHelpr.close();
        super.onDestroy();
    }
}